layui.use(['form'], function () {
  var form = layui.form,
      layer = layui.layer;
  //配置弹窗风格
  layer.config({
    extend: 'myskin/style.css'
  });
  // 弹窗集合
  var _html = '';
  _html += '';
  //登录
  _html +='<div class="sign-box" id="signInBox">';
  _html +='  <ul class="sign-tab">';
  _html +='    <li><a href="javascript:;" class="btn_signIn active">登录</a></li>';
  _html +='    <li><a href="javascript:;" class="btn_register">注册</a></li>';
  _html +='  </ul>';
  _html +='  <div class="sign-body">';
  _html +='    <form class="layui-form">';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-icon">';
  _html +='          <i class="icon icon-1"></i>';
  _html +='        </div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="text" name="username" id="login_username" placeholder="请输入手机号/邮箱" class="layui-input">';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-icon">';
  _html +='          <i class="icon icon-2"></i>';
  _html +='        </div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="password" name="userpass" id="login_userpass" placeholder="请输入密码" class="layui-input">';
  _html +='          <span class="pass-switch" id="pass_switch"><i class="icon-eye"></i></span>';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item auto-item layui-clear">';
  _html +='        <div class="layui-input-icon"></div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="checkbox" name="auto-cbx1" checked class="auto-cbx" id="auto-cbx1">';
  _html +='          <label class="auto-sign" for="auto-cbx1">7天内自动登录</label>';
  _html +='          <a href="javascript:;" class="btn-link btn-forget-pass" id="loginForgetPwd">忘记密码？</a>';
  _html +='        </div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item" style="margin-top: 26px">';
  _html +='        <div class="layui-input-icon"></div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <button class="layui-btn layui-btn-normal layui-btn-radius" id="login_submit">登录</button>';
  _html +='        </div>';
  _html +='      </div>';
  _html +='    </form>';
  _html +='  </div>';
  _html +='  <div class="sign-extra">';
  _html +='    <div class="es-tips">使用其他账号登录</div>';
  _html +='    <div class="es-bar">';
  _html +='      <a href="javascript:;" class="link-wexin">';
  _html +='        <i class="icon-wexin"></i>';
  _html +='      </a>';
  _html +='    </div>';
  _html +='  </div>';
  _html +='</div>';
  //注册
  _html +='<div class="sign-box" id="registerBox" style="display: none">';
  _html +='  <ul class="sign-tab">';
  _html +='    <li><a href="javascript:;" class="btn-signIn">登录</a></li>';
  _html +='    <li><a href="javascript:;" class="btn_register active">注册</a></li>';
  _html +='  </ul>';
  _html +='  <div class="sign-body">';
  _html +='    <form class="layui-form">';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-icon">';
  _html +='          <i class="icon icon-1"></i>';
  _html +='        </div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="text" name="username" id="reg_phone" placeholder="请输入手机号" class="layui-input">';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-icon">';
  _html +='          <i class="icon icon-3"></i>';
  _html +='        </div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="text" name="useremail" id="reg_email" placeholder="请输入常用邮箱" class="layui-input">';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-icon"></div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="text" name="usercode" id="reg_code" style="width: 230px" placeholder="请输入验证码" class="layui-input">';
  _html +='        </div>';
  _html +='        <button class="layui-btn layui-btn-normal get-code" id="get_reg_code">获取验证码</button>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-icon"></div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <i class="icon-pass"></i>';
  _html +='          <input type="password" name="userpass" id="reg_pwd" placeholder="请输入新密码" class="layui-input2">';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item auto-item layui-clear">';
  _html +='        <div class="layui-input-icon"></div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="checkbox" class="auto-cbx" id="auto_cbx2">';
  _html +='          <label class="auto-sign" for="auto_cbx2">我已阅读并同意<a href="#">服务协议</a></label>';
  _html +='        </div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item" style="margin-top: 26px">';
  _html +='        <div class="layui-input-icon"></div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <button class="layui-btn layui-btn-normal layui-btn-radius btn-reg layui-btn-disabled" id="btn_reg">注册</button>';
  _html +='        </div>';
  _html +='      </div>';
  _html +='    </form>';
  _html +='  </div>';
  _html +='</div>';
  //绑定手机号
  _html +='<div class="sign-box" id="bindPhoneBox" style="display: none">';
  _html +='  <div class="sign-tips">应《中华人民共和国网络安全法》要求，为了更好保障您的帐号安全，请绑定您的手机号。</div>';
  _html +='  <div class="sign-body">';
  _html +='    <form class="layui-form">';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="text" name="username" id="bindPhone" placeholder="请输入手机号" class="layui-input">';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="text" name="usercode" id="bindCode" style="width: 230px" placeholder="请输入验证码" class="layui-input">';
  _html +='        </div>';
  _html +='        <button class="layui-btn layui-btn-normal get-code" id="get_bind_code">获取验证码</button>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item" style="margin-top: 10px">';
  _html +='        <button class="layui-btn layui-btn-normal layui-btn-radius" id="btn_bindPhone">绑定</button>';
  _html +='      </div>';
  _html +='    </form>';
  _html +='  </div>';
  _html +='</div>';
  //找回密码
  _html +='<div class="sign-box" id="findPwdBox" style="display: none">';
  _html +='  <div class="sign-title">忘记密码</div>';
  _html +='  <div class="sign-body">';
  _html +='    <form class="layui-form">';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-icon">';
  _html +='          <i class="icon icon-1"></i>';
  _html +='        </div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="text" name="username" id="find_username" placeholder="请输入手机号" class="layui-input">';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-icon">';
  _html +='          <i class="icon icon-2"></i>';
  _html +='        </div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="text" name="usercode1" id="find_usercode1" style="width: 140px" placeholder="请输入检验码" class="layui-input">';
  _html +='        </div>';
  _html +='        <a href="javascript:;" class="get-pic" id="btn_findCode1">';
  _html +='          <img id="imgC" alt="图片"/>';
  _html +='        </a>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-icon">';
  _html +='          <i class="icon icon-2"></i>';
  _html +='        </div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="text" name="usercode2" id="find_usercode2" style="width: 230px" placeholder="请输入验证码" class="layui-input">';
  _html +='        </div>';
  _html +='        <button class="layui-btn layui-btn-normal get-code" id="btn_findCode2">获取验证码</button>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item" style="margin-top: 10px">';
  _html +='        <div class="layui-input-icon"></div>';
  _html +='        <div class="layui-input-inline">';
  _html +='          <button class="layui-btn layui-btn-normal layui-btn-radius" id="btn_findpwd">下一步</button>';
  _html +='        </div>';
  _html +='      </div>';
  _html +='    </form>';
  _html +='  </div>';
  _html +='</div>';
  //设置密码
  _html +='<div class="sign-box" id="setPwdBox" style="display: none">';
  _html +='  <div class="sign-title">设置密码</div>';
  _html +='  <div class="sign-body">';
  _html +='    <form class="layui-form">';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-inline">';
  _html +='          <i class="icon-pass"></i>';
  _html +='          <input type="password" name="userpass1" id="userpass1" placeholder="请输入新密码" class="layui-input2">';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-inline">';
  _html +='          <i class="icon-pass"></i>';
  _html +='          <input type="password" name="userpass2" id="userpass2" placeholder="请再次输入新密码" class="layui-input2">';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item" style="margin-top: 10px">';
  _html +='        <button class="layui-btn layui-btn-normal layui-btn-radius" id="btn_setPwd">提交</button>';
  _html +='      </div>';
  _html +='    </form>';
  _html +='  </div>';
  _html +='</div>';



  //更改手机号
  _html +='<div class="sign-box" id="changePhoneBox" style="display: none">';
  _html +='  <div class="sign-tips">解绑后，您将无法再使用此手机号进行找回密码及登录相关操作。</div>';
  _html +='  <div class="sign-body">';
  _html +='    <form class="layui-form">';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-inline">';
  _html +='          <input type="text" name="userphone" id="unbindPhone" placeholder="请输入已绑定手机号" class="layui-input">';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item" style="margin-top: 10px">';
  _html +='        <button class="layui-btn layui-btn-normal layui-btn-radius" id="btn_unbindPhone">下一步</button>';
  _html +='      </div>';
  _html +='    </form>';
  _html +='  </div>';
  _html +='</div>';

  //修改密码
  _html +='<div class="sign-box" id="modifyPwdBox" style="display: none">';
  _html +='  <div class="sign-title">修改密码</div>';
  _html +='  <div class="sign-body">';
  _html +='    <form class="layui-form">';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-inline">';
  _html +='          <i class="icon-pass"></i>';
  _html +='          <input type="password" name="userpwd1" id="modifyPwd1" placeholder="请输入原密码" class="layui-input2">';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-inline">';
  _html +='          <i class="icon-pass"></i>';
  _html +='          <input type="password" name="userpwd2" id="modifyPwd2" placeholder="请输入新密码" class="layui-input2">';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item">';
  _html +='        <div class="layui-input-inline">';
  _html +='          <i class="icon-pass"></i>';
  _html +='          <input type="password" name="userpwd3" id="modifyPwd3" placeholder="请再次输入新密码" class="layui-input2">';
  _html +='        </div>';
  _html +='        <div class="layui-tips layui-clear"></div>';
  _html +='      </div>';
  _html +='      <div class="layui-form-item" style="margin-top: 10px">';
  _html +='        <button class="layui-btn layui-btn-normal layui-btn-radius" id="btn_modifyPwd">提交</button>';
  _html +='      </div>';
  _html +='    </form>';
  _html +='  </div>';
  _html +='</div>';

  var emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var phoneReg = /^1\d{10}$/;
  var password = /^[\S]{8,20}$/;

  window.showPopup = function (type) {
    var _content = type + 'Box';
    layer.open({
      type: 1,
      title: false,
      anim: 0,
      area:'620px',
      resize: false,
      content: _html,
      skin: 'layer-ext-moon',
      success: function (layero, index) {
        layero.find('#' + _content).show().siblings('.sign-box').remove();
        switch (type){
          case "signIn":
            signInEvent(layero, index);
            break;
          case "register":
            registerEvent(layero, index);
            break;
          case "findPwd":
            findPwdEvent(layero, index);
            break;
          case "setPwd":
            setPwdEvent(layero, index);
            break;
          case "changePhone":
            changePhoneEvent(layero, index);
            break;
          case "modifyPwd":
            modifyPwdEvent(layero, index);
            break;
          case "bindPhone":
            bindPhoneEvent(layero, index);
            break;
        }
      }
    });
  }
  //登录
  function signInEvent(layero, index) {
    $('#loginForgetPwd').on('click', function () {
      layer.close(index);
      showPopup('findPwd');
    });
    $('#pass_switch').on('click', function () {
      var $input = $(this).siblings('.layui-input');
      $(this).toggleClass('active');
      $(this).hasClass('active') ? $input.attr('type', 'text') : $input.attr('type', 'password');
    });
    $('#login_username, #login_userpass').on('focus', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      $parents.find('.layui-tips').html('');
    });
    $('#login_username').on('blur', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      var _val = $this.val();
      if(!emailReg.test(_val) && !phoneReg.test(_val)) {
        $parents.find('.layui-tips').html('请输入正确的手机号或邮箱');
      }
    });
    $('#login_userpass').on('blur', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      var _val = $this.val();
      if(!password.test(_val)) {
        $parents.find('.layui-tips').html('请输入正确的密码');
      }
    });
    $('#login_submit').on('click', function (e) {
      e.preventDefault();
      var _userName = $('#login_username').val(),
          _userPwd  = $('#login_userpass').val(),
          _autoCbx  = $('#auto-cbx1:checked').val();
      var $nameParent = $('#login_username').parents('.layui-form-item'),
          $pwdParent = $('#login_userpass').parents('.layui-form-item');
      if(typeof _userName == '' || !emailReg.test(_userName) && !phoneReg.test(_userName)) {
        $nameParent.find('.layui-tips').text('请输入正确的手机号或邮箱');
        return;
      }
      if(typeof _userPwd == '' || !password.test(_userPwd)) {
        $pwdParent.find('.layui-tips').text('请输入正确的密码');
        return;
      }
      var _url = '/passport/login';
      var _data = {username: _userName, userpass: _userPwd, autocbx: _autoCbx};
      $.post(_url, _data, function (response){
        if(response.status == 1) {
          layer.close(index);
          layer.msg(response.msg, function () {
            location.href = response.href;
          });
        }else{
          layer.msg(response.msg);
        }
      }, 'json')
    });
    $('.btn_register').on('click', function () {
      layer.close(index);
      showPopup('register');
    });
  }
  //注册
  function registerEvent(layero, index) {
    $('#reg_phone, #reg_email, #reg_code, #reg_pwd').on('focus', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      $parents.find('.layui-tips').html('');
    });
    $('#reg_phone').on('blur', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      var _val = $this.val();
      if(typeof _val === 'string' && _val.length) {
        if(phoneReg.test(_val)){
          // 检测手机号是否可用
          $.post('/passport/check_phone', {phone: _val}, function (response) {
            if(response.status == 1) {
              // do something
            }else {
              $parents.find('.layui-tips').html(response.msg);
            }
          }, 'json');
        }else  {
          $parents.find('.layui-tips').html('请输入正确的手机号');
        }
      }
    });
    $('#reg_email').on('blur', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      var _val = $this.val();
      if(typeof _val === 'string' && _val.length) {
        if(emailReg.test(_val)){
          // 检测手机号是否可用
          $.post('/passport/check_email', {email: _val}, function (response) {
            if(response.status == 1) {
              // do something
            }else {
              $parents.find('.layui-tips').html(response.msg);
            }
          }, 'json');
        }else  {
          $parents.find('.layui-tips').html('请输入正确的邮箱');
        }
      }
    });
    $('#get_reg_code').on('click', function (e) {
      e.preventDefault();
      var $this = $(this);
      var _phone = $('#reg_phone').val();
      var $phoneParent = $('#reg_phone').parents('.layui-form-item');
      if(typeof _phone == 'undefined' || _phone == '') {
        $phoneParent.find('.layui-tips').html('请输入手机号码');
        return;
      }
      if(!phoneReg.test(_phone)) {
        $phoneParent.find('.layui-tips').html('手机号输入有误');
        return;
      }
      if(!$this.hasClass('layui-btn-disabled')) {
        $.ajax({
          url: '/Passport/sendCode',
          type: 'post',
          data: {phone: _phone},
          dataType: 'json',
          beforeSend: function () {
            $this.addClass('layui-btn-disabled');
          },
          success: function (response) {
            if(response.status == 1) {
              var time = 60;
              $this.html(time + 's后重新获取');
              var _inv = setInterval(function () {
                if(time > 0) {
                  time --;
                  $this.html(time + 's后重新获取');
                }else {
                  $this.removeClass('layui-btn-disabled').html('获取验证码');
                  clearInterval(_inv);
                }
              }, 1000);
            }else {
              layer.msg(response.msg);
              $this.removeClass('layui-btn-disabled');
            }
          }
        });
      }
    });
    $('#reg_pwd').on('blur', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      var _val = $this.val();
      if(!password.test(_val)) {
        $parents.find('.layui-tips').html('请输入8至20位数字或英文密码');
      }
    });
    $('#auto_cbx2').on('change', function () {
      var $this = $(this);
      var isChecked = $this.prop('checked');
      if(!isChecked) {
        $('#btn_reg').addClass('layui-btn-disabled');
      }else {
        $('#btn_reg').removeClass('layui-btn-disabled');
      }
    });
    $('#btn_reg').on('click', function (e) {
      e.preventDefault();
      var $this = $(this);
      if(!$this.hasClass('layui-btn-disabled')){
        var _phone = $('#reg_phone').val(),
            _email = $('#reg_email').val(),
            _code  = $('#reg_code').val(),
            _pwd   = $('#reg_pwd').val();
        var $phoneParent = $('#reg_phone').parents('.layui-form-item'),
            $emailParent = $('#reg_email').parents('.layui-form-item'),
            $codeParent = $('#reg_code').parents('.layui-form-item'),
            $pwdParent = $('#reg_pwd').parents('.layui-form-item');
        if(typeof _phone === 'undefined' || _phone == '' || !phoneReg.test(_phone)) {
          $phoneParent.find('.layui-tips').html('请输入正确的手机号');
          return;
        }
        if(typeof _email === 'undefined' || _email == '' || !emailReg.test(_email)) {
          $emailParent.find('.layui-tips').html('请输入正确的邮箱');
          return ;
        }
        if(typeof _code === 'undefined' || _code == '') {
          $codeParent.find('.layui-tips').html('请输入验证码');
          return;
        }
        if(typeof _pwd === 'undefined' || _pwd == '' || !password.test(_pwd)) {
          $pwdParent.find('.layui-tips').html('请输入8至20位数字或英文密码');
          return;
        }
        $.ajax({
          url: '/passport/register',
          data: {phone: _phone, email: _email, code: _code, pwd: _pwd},
          type: 'post',
          dataType: 'json',
          success: function (response) {
            if(response.status == 1) {
              layer.close(index);
              layer.msg(response.msg, function () {
                location.href = response.href;
              });
            }else {
              layer.msg(response.msg);
            }
          }
        })
      }
    })
    $('.btn-signIn').on('click', function () {
      layer.close(index);
      showPopup('signIn');
    });
  }
  //找回密码
  function findPwdEvent(layero, index) {
    $('#find_username, #find_usercode1, #find_usercode2').on('focus', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      $parents.find('.layui-tips').html('');
    });
    $('#find_username').on('blur', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      var _val = $this.val();
      if(!emailReg.test(_val) && !phoneReg.test(_val)) {
        $parents.find('.layui-tips').html('请输入正确的手机号');
      }
    });
    $('#btn_findCode1').on('click', function (e) {
      e.preventDefault();
      getPic();
    });
    getPic();
    $('#btn_findCode2').on('click', function (e) {
      e.preventDefault();
      var $this = $(this);
      var _phone = $('#find_username').val();
      var $phoneParent = $('#find_username').parents('.layui-form-item');
      if(typeof _phone == 'undefined' || _phone == '') {
        $phoneParent.find('.layui-tips').html('请输入手机号码');
        return;
      }
      if(!phoneReg.test(_phone)) {
        $phoneParent.find('.layui-tips').html('手机号输入有误');
        return;
      }
      if(!$this.hasClass('layui-btn-disabled')) {
        $.ajax({
          url: '/passport/sendFindpwdVerify',
          type: 'post',
          data: {phone: _phone},
          dataType: 'json',
          beforeSend: function () {
            $this.addClass('layui-btn-disabled');
          },
          success: function (response) {
            if(response.status == 1) {
              var time = 60;
              $this.html(time + 's后重新获取');
              var _inv = setInterval(function () {
                if(time > 0) {
                  time --;
                  $this.html(time + 's后重新获取');
                }else {
                  $this.removeClass('layui-btn-disabled').html('获取验证码');
                  clearInterval(_inv);
                }
              }, 1000);
            }else {
              layer.msg(response.msg);
              $this.removeClass('layui-btn-disabled');
            }
          }
        });
      }
    });
    $('#btn_findpwd').on('click', function (e) {
      e.preventDefault();
      var _username = $('#find_username').val(),
          _usercode1 = $('#find_usercode1').val(),
          _usercode2 = $('#find_usercode2').val();
      var $userNameParent = $('#find_username').parents('.layui-form-item'),
          $userCode1Parent = $('#find_usercode1').parents('.layui-form-item'),
          $userCode2Parent = $('#find_usercode2').parents('.layui-form-item');
      if(typeof _username == 'undefined' || _username == '') {
        $userNameParent.find('.layui-tips').html('请输入手机号');
        return;
      }else if(!phoneReg.test(_username)){
        $userNameParent.find('.layui-tips').html('请填写正确的手机号');
        return;
      }
      if(typeof _usercode1 == 'undefined' || _usercode1 == '') {
        $userCode1Parent.find('.layui-tips').html('请填写图片验证码');
        return;
      }
      if(typeof _usercode2 == 'undefined' || _usercode2 == '') {
        $userCode2Parent.find('.layui-tips').html('请填写验证码');
        return;
      }
      var _url = '/passport/checkPhoneByFindPwd',
        _data ={username: _username, usercode1: _usercode1, usercode2: _usercode2};
      $.post(_url, _data, function (response){
        if(response.status == 1){
          layer.close(index);
          showPopup('setPwd');
        }else{
          layer.msg(response.msg);
        }
      }, 'json')
    });
  }
  //检验码
  function getPic(){
    $.post('/passport/showCaptcha', {}, function (response){
      if(response.status == 1) {
        $("#imgC").attr("src", response.image)
      }else {
        layer.msg(response.msg)
      }
    }, 'json');
  }
  //设置密码
  function setPwdEvent(layero, index) {
    $('#userpass1, #userpass2').on('focus', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      $parents.find('.layui-tips').html('');
    });
    $('#userpass1').on('blur', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      var _val = $this.val();
      if(!password.test(_val)) {
        $parents.find('.layui-tips').html('请输入8至20位数字或英文密码');
      }
    });
    $('#userpass2').on('blur', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      var _val = $this.val();
      var _pass1 = $('#userpass1').val();
      if(_val != _pass1) {
        $parents.find('.layui-tips').html('密码输入不一致');
      }
    });
    $('#btn_setPwd').on('click', function (e) {
      e.preventDefault();
      var _userPwd1 = $('#userpass1').val(),
          _userPwd2 = $('#userpass2').val();
      var $pwd1Parent = $('#userpass1').parents('.layui-form-item'),
          $pwd2Parent = $('#userpass2').parents('.layui-form-item');
      if(typeof _userPwd1 == 'undefined' || _userPwd1 == '' || !password.test(_userPwd1)) {
        $pwd1Parent.find('.layui-tips').html('请输入正确的密码');
        return;
      }
      if(typeof _userPwd2 == 'undefined' || _userPwd2 == '') {
        $pwd2Parent.find('.layui-tips').html('请确认密码');
        return;
      }
      if(_userPwd2 != _userPwd1) {
        $pwd2Parent.find('.layui-tips').html('密码输入不一致');
        return;
      }
      $.post('/passport/resetPasswd', {pwd1: _userPwd1, pwd2: _userPwd2}, function (response){
        if(response.status == 1) {
          layer.close(index);
          layer.msg(response.msg, function () {
            location.href = response.href;
          });
        }
      }, 'json')
    })
  }
  //绑定手机号
  function bindPhoneEvent(layero, index) {
    $('#bindPhone, #bindCode').on('focus', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      $parents.find('.layui-tips').html('');
    });
    $('#bindPhone').on('blur', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      var _val = $this.val();
      if(!phoneReg.test(_val)) {
        $parents.find('.layui-tips').html('请输入正确的手机号');
      }
    });
    $('#get_bind_code').on('click', function (e) {
      e.preventDefault();
      var $this = $(this);
      var _phone = $('#bindPhone').val();
      var $phoneParent = $('#bindPhone').parents('.layui-form-item');
      if(typeof _phone == 'undefined' || _phone == '') {
        $phoneParent.find('.layui-tips').html('请输入手机号码');
        return;
      }
      if(!phoneReg.test(_phone)) {
        $phoneParent.find('.layui-tips').html('手机号输入有误');
        return;
      }
      if(!$this.hasClass('layui-btn-disabled')) {
        $.ajax({
          url: '/passport/sendCode',
          type: 'post',
          data: {phone: _phone,type:1},
          dataType: 'json',
          beforeSend: function () {
            $this.addClass('layui-btn-disabled');
          },
          success: function (response) {
            if(response.status == 1) {
              var time = 60;
              $this.html(time + 's后重新获取');
              var _inv = setInterval(function () {
                if(time > 0) {
                  time --;
                  $this.html(time + 's后重新获取');
                }else {
                  $this.removeClass('layui-btn-disabled').html('获取验证码');
                  clearInterval(_inv);
                }
              }, 1000);
            }else {
              layer.msg(response.msg);
              $this.removeClass('layui-btn-disabled');
            }
          }
        });
      }
    });
    $('#btn_bindPhone').on('click', function (e) {
      e.preventDefault();
      var _phone = $('#bindPhone').val(),
          _code  = $('#bindCode').val();
      var $phoneParent = $('#bindPhone').parents('.layui-form-item'),
          $codeParent = $('#bindCode').parents('.layui-form-item');
      if(typeof _phone == '' || !phoneReg.test(_phone)) {
        $phoneParent.find('.layui-tips').text('请输入正确的手机号');
        return;
      }
      if(typeof _code === 'undefined' || _code == '') {
        $codeParent.find('.layui-tips').text('请输入验证码');
        return;
      }
      $.post('/my/update_student_phone', {phone: _phone, code: _code}, function (response){
        if(response.status == 1) {
          layer.close(index);
          layer.msg(response.msg, function () {
            location.href = '';
          });
        }else{
          layer.msg(response.msg);
        }
      }, 'json')
    });
  }


  //更换手机号
  function changePhoneEvent(layero, index) {
    var $changePhoneBox = layero.find('#changePhoneBox');
    $('#unbindPhone').on('focus', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      $parents.find('.layui-tips').html('');
    });
    $('#unbindPhone').on('blur', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      var _val = $this.val();
      if(!phoneReg.test(_val)) {
        $parents.find('.layui-tips').html('请输入正确的手机号');
      }
    });
    $('#btn_unbindPhone').on('click', function (e) {
      e.preventDefault();
      var _userPhone = $('#unbindPhone').val();
      var $Parent = $('#unbindPhone').parents('.layui-form-item');
      if(typeof _userPhone == '' ||  !phoneReg.test(_userPhone)) {
        $Parent.find('.layui-tips').text('请输入正确的手机号');
        return;
      }
      var _url = '/my/check_student_phone';
      var _data = {userphone: _userPhone};
      $.post(_url, _data, function (response){
        if(response.status == 1) {
          layer.close(index);
          showPopup('bindPhone');
        }else{
          layer.msg(response.msg);
        }
      }, 'json')
    });
  }
  //修改密码
  function modifyPwdEvent(layero, index) {
    $('#modifyPwd1, #modifyPwd2, #modifyPwd3').on('focus', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      $parents.find('.layui-tips').html('');
    });
    $('#modifyPwd1, #modifyPwd2').on('blur', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      var _val = $this.val();
      if(!password.test(_val)) {
        $parents.find('.layui-tips').html('请设置8至20位数字或英文密码');
      }
    });
    $('#modifyPwd3').on('blur', function () {
      var $this = $(this);
      var $parents = $this.parents('.layui-form-item');
      var _val = $this.val();
      var _pass2 = $('#modifyPwd2').val();
      if(_val != _pass2) {
        $parents.find('.layui-tips').html('密码输入不一致');
      }
    });
    $('#btn_modifyPwd').on('click', function (e) {
      e.preventDefault();
      var _userPwd1 = $('#modifyPwd1').val(),
          _userPwd2 = $('#modifyPwd2').val(),
          _userPwd3 = $('#modifyPwd3').val();
      var $pwd1Parent = $('#modifyPwd1').parents('.layui-form-item'),
          $pwd2Parent = $('#modifyPwd2').parents('.layui-form-item'),
          $pwd3Parent = $('#modifyPwd3').parents('.layui-form-item');
      if(typeof _userPwd1 == 'undefined' || _userPwd1 == '' || !password.test(_userPwd1)) {
        $pwd1Parent.find('.layui-tips').html('请输入正确的密码');
        return;
      }
      if(typeof _userPwd2 == 'undefined' || _userPwd2 == '' || !password.test(_userPwd2)) {
        $pwd2Parent.find('.layui-tips').html('请输入正确的密码');
        return;
      }
      if(typeof _userPwd3 == 'undefined' || _userPwd3 == '') {
        $pwd3Parent.find('.layui-tips').html('请确认密码');
        return;
      }
      if(_userPwd3 != _userPwd2) {
        $pwd3Parent.find('.layui-tips').html('密码输入不一致');
        return;
      }
      $.post('/my/set_new_password', {pwd1: _userPwd1, pwd2: _userPwd2, pwd3: _userPwd3}, function (response){
        if(response.status == 1) {
          layer.close(index);
          layer.msg(response.msg, function () {
            location.href = response.href;
          });
        }else{
          layer.msg(response.msg);
        }
      }, 'json')
    })
  }


  $('.js-header-signIn').on('click', function () {
    showPopup('signIn');
  });

  //检查登陆
  $('.js-checked-signIn').on('click', function () {
    showPopup('signIn');
    return false;
  });

  $('.js-header-register').on('click', function () {
    showPopup('register');
  });

  $('.op-bindphone').on('click', function () {
    showPopup('bindPhone');
  });


  //修改手机
  $('.js-info-phone').on('click', function () {
    showPopup('changePhone');
  });

  //修改密码
  $('.js-info-pwd').on('click', function () {
    showPopup('modifyPwd');
  });


});